import { ref, reactive, onMounted } from 'vue';
import router from '@/router/index'
import { showDialog } from 'vant'
import { Addorder } from '@/api/api'
import { usePayStore } from '@/store/goPay'


//自提和外送 公用hooks
export function useConfirm() {
    const time = ref(15 * 60 * 1000);
    const checked = ref(true);
    const show = ref(true);

    //显示与隐藏优惠
    const Discount = () => {
        if (show.value === true) {
            show.value = false
        } else {
            show.value = true
        }
    }

    // //跳转到支付页面 判断当前时间是否营业
    const gobuy = async () => {
        const Times = new Date()
        const hours = Times.getHours()



        if (hours < 10 || hours > 17) {

            showDialog({
                message: `店铺已打烊ZzZi，
                10:00-18:00营业时间再来吧~`,
            }).then(() => {
                // 点击确认，跳到 首页
                router.push('home')
            });


        } else {
            const stores = usePayStore()
            console.log(stores.orderData);
            stores.orderData.forEach(async(item) => {
                let res = await Addorder({ uId: 11,address:item.order_address,proId:item.goods_id,goods_num:item.goods_num})
               console.log(res);
               
            });
                      
            router.push({ path: '/cartPay', query: { url: '/ConfirmOrder' } })   

            //
        }
    }


    return {
        time,
        checked,
        show,
        Discount,
        gobuy

    }
}




